

#import "MyContactListener.h"
#import "Box2D.h"
#import "HelloWorldLayer.h"
#import "ContactTypes.h"

MyContactListener::MyContactListener(HelloWorldLayer* callback){
    helloWorldLayer = callback;
}

MyContactListener::~MyContactListener(){}

void MyContactListener::BeginContact(b2Contact* contact){
    
    void* fixtureUserDataA = contact->GetFixtureA()->GetUserData();
    void* fixtureUserDataB = contact->GetFixtureB()->GetUserData();
    
    if( (int)fixtureUserDataA == PLATFORM_CONTACT || (int) fixtureUserDataB == PLATFORM_CONTACT) {
        numFootContacts++;
        [ helloWorldLayer groundContact];
    }
    
    
    if( ((int)fixtureUserDataB == PLATFORM_CONTACT && ( (int) fixtureUserDataA > 6 && (int) fixtureUserDataA <107))
       || ((int)fixtureUserDataA == PLATFORM_CONTACT && ( (int) fixtureUserDataB > 6 && (int) fixtureUserDataB <107)) ){
    
        [ helloWorldLayer enemyContact];
    
    }else if( (int)fixtureUserDataA == FINISH_CONTACT || (int)fixtureUserDataB == FINISH_CONTACT) {
        [helloWorldLayer endLvl];
    }
    else if( ( (int) fixtureUserDataA > 6 && (int) fixtureUserDataA <107) && ((int)fixtureUserDataB == BLOCKER_CONTACT) ) {
        int index = (int) fixtureUserDataA -7;
        enemyDirection[index] = !enemyDirection[index];   
       
    }else if( ( (int) fixtureUserDataB > 6 && (int) fixtureUserDataB <107) && ((int)fixtureUserDataA == BLOCKER_CONTACT) ) {
        int index = (int) fixtureUserDataB-7;
        enemyDirection[index] = !enemyDirection[index];
        
    }
    
}

void MyContactListener::EndContact(b2Contact* contact){
    void* fixtureUserData = contact->GetFixtureA()->GetUserData();
    if((int) fixtureUserData == PLATFORM_CONTACT) {
        numFootContacts--;
    }
    
    fixtureUserData = contact->GetFixtureB()->GetUserData();
    if((int) fixtureUserData == PLATFORM_CONTACT) {
        numFootContacts--;
    }
}
